package com.example.flowableoa.controller;



import com.example.comment_oa.response.AjaxResponse;

import com.example.flowableoa.domain.dto.FlowDeployProcDto;
import com.example.flowableoa.service.IflowDefinitionService;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Slf4j
@RestController
@CrossOrigin
@RequestMapping("flowableOa")
public class FlowDefinitionController {

    @Autowired
    private IflowDefinitionService iflowDefinitionService;



    /**
     * 部署流程操作
     * @return
     */
    @PostMapping("/deployFlow")
    public AjaxResponse deployFlow(@RequestParam(value = "name" ,required = false) String name,
                                   @RequestParam(value = "categroy",required = false) String categroy,
                                   MultipartFile file){
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
            iflowDefinitionService.deployFlow(name,categroy,inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return AjaxResponse.success();
    }


    /**
     * *
     * 查询定义的流程
     */
    @GetMapping("/list")
    public AjaxResponse sleectall(){
        List<FlowDeployProcDto> lsit = iflowDefinitionService.list();
        return AjaxResponse.success(lsit);
    }

    @GetMapping("/list2/{guanjianzi}")
    public AjaxResponse sleectall2(@PathVariable("guanjianzi")String guanjianzi){
        List<FlowDeployProcDto> lsit = iflowDefinitionService.list2(guanjianzi);
        return AjaxResponse.success(lsit);
    }

    /*
    * 更新流程的状态
     */
    @GetMapping("/updateState/{definitionId}")
    public AjaxResponse updateState(@PathVariable("definitionId")String definitionId){
        iflowDefinitionService.updatestate(definitionId);
        return AjaxResponse.success();
    }

    /*
     * 删除流程
     */
    @DeleteMapping("/deleteFlow/{deployId}")
    public AjaxResponse deleteFlow(@PathVariable("deployId")String deployId){
        iflowDefinitionService.deleteFlow(deployId);
        return AjaxResponse.success();
    }


    /*
    *  查看流程图的方法
     */
    @PostMapping(value = "/flowImg/{deployId}")
//    @ResponseBody
    public AjaxResponse flowImg(@PathVariable("deployId") String deployId){
        System.err.println("图片的ID："+deployId);
        byte[] bytes = iflowDefinitionService.flowImg(deployId);
        System.err.println("字节码："+bytes.length);
        System.err.println("字节码："+bytes);
        return new AjaxResponse(bytes);
    }

}
